package tray;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.internal.theme.Theme;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;

import com.cloudgarden.resource.SWTResourceManager;


public class TrayIcon implements Runnable{
	private Display display;
	private Shell shell;
	private Image connectedImage;
	private Tray tray;
	private TrayItem item;
	private Menu menu;
	private MenuItem close;
	private ToolTip tooltip;
	private boolean isTransfer;
	private Image transfering1;
	private Image transfering2;
	private Image notConnectedImage;

	public static void main(String[] str){
		new TrayIcon().run();
	}
	public TrayIcon(){
		display = null;
		shell= null;
		connectedImage= null;
		tray= null;
		item= null;
		menu= null;
		close= null;
		tooltip= null;
		isTransfer= false;
		transfering1= null;
		transfering2= null;
		notConnectedImage= null;

	}
	public void run() {
		isTransfer=false;
		display = new Display();
		shell = new Shell(display);
		connectedImage = SWTResourceManager.getImage("icons\\connectedImage.png");//new Image(display, "icons\\connectedImage.png");
		transfering1 = SWTResourceManager.getImage("icons\\transfering1.png");//new Image(display, "icons\\transfering1.png");
		transfering2 = SWTResourceManager.getImage("icons\\transfering2.png");//new Image(display, "icons\\transfering2.png");
		notConnectedImage = SWTResourceManager.getImage("icons\\notConnectedImage.png");//new Image(display,"icons\\notConnectedImage.png");
		tray = display.getSystemTray();

		if (tray == null) {
			System.out.println("The system tray is not available");
		} 
		else {
			item = new TrayItem(tray, SWT.NONE);
			item.setToolTipText("Clever DropBox");
			tooltip = new ToolTip(shell, SWT.BALLOON);
			tooltip.setAutoHide(true);
			item.setToolTip(tooltip);
			tooltip.setVisible(false);
			menu = new Menu(shell, SWT.POP_UP);
			close = new MenuItem(menu, SWT.PUSH);
			close.setText("Close");
			close.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event event) {
					System.out.println("selection " + event.widget);
					System.exit(0);
				}
			});

			menu.setDefaultItem(close);

			item.addListener(SWT.MenuDetect, new Listener() {
				public void handleEvent(Event event) {
					menu.setVisible(true);
				}
			});
			item.setImage(connectedImage);
		}
		System.out.println("trayIcon $#%#$%#%#$%#$%#");
		//shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		connectedImage.dispose();
		display.dispose();
		System.out.println("trayIcon end $#%#$%#%#$%#$%#");
	}

	public void writeMessage(final String title,final String message){
		Display display = Display.getDefault();
		display.asyncExec(new Runnable() {

			@Override
			public void run() {
				tooltip.setText(title);
				tooltip.setMessage(message);
				tooltip.setVisible(true);
			}
		});
	}

	/*	public void startTransfer(){
		Display display = Display.getDefault();
		display.asyncExec(new Runnable() {

			@Override
			public void run() {
				isTransfer=true;
				int counter=0;
				while(isTransfer){
					if(counter%2==0)
						item.setImage(transfering1);
					else
						item.setImage(transfering2);
					shell.pack();
					counter++;
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				item.setImage(connectedImage);
				shell.pack();
			}
		});
	}*/
	/*public void stopTransfering() {
		isTransfer=false;
	}*/

	public void connected(){
		Display display = Display.getDefault();
		display.asyncExec(new Runnable() {

			@Override
			public void run() {
				item.setImage(connectedImage);
				shell.pack();
			}
		});
	}

	public void notConnected(){
		Display display = Display.getDefault();
		display.asyncExec(new Runnable() {

			@Override
			public void run() {
				item.setImage(notConnectedImage);
				shell.pack();
			}
		});
	}
}
